package com.webchat.chat.service.impl;

import com.webchat.chat.core.AbstractService;
import com.webchat.chat.core.enums.SocketClientType;
import com.webchat.chat.core.enums.SocketRoomStatus;
import com.webchat.chat.core.redis.RedisService;
import com.webchat.chat.core.tools.RedisConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.dao.SocketRoomMapper;
import com.webchat.chat.model.SocketRoom;
import com.webchat.chat.service.SocketRoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by admin on 2018-06-29.
 */
@Service
@Transactional
public class SocketRoomServiceImpl extends AbstractService<SocketRoom> implements SocketRoomService {
    @Resource
    private SocketRoomMapper socketRoomMapper;

    @Resource
    private RedisService redisService;

    @Override
    public List<SocketRoom> findSocketRooms(String clientId, String tenantCode, SocketRoomStatus status) {
        return socketRoomMapper.selectSocketRooms(clientId, tenantCode, status);
    }

    @Override
    public SocketRoom findSocketRoomByRoomId(String roomId) {
        return socketRoomMapper.selectSocketRoomByRoomId(roomId);
    }

    @Override
    public List<SocketRoom> getChatRoom(String tenantCode) {
        return socketRoomMapper.getChatRoom(tenantCode);
    }

    @Override
    public SocketRoom findbyTenantCode(String tenantCode) {
        SocketRoom room = new SocketRoom();
        room.setTenantCode(tenantCode);
        room.setType(SocketClientType.聊天室);
        return socketRoomMapper.selectOne(room);
    }

    @Override
    public void changePublicRoomToNotice(String roomIds, Boolean flag) {
        List<SocketRoom> socketRooms = socketRoomMapper.selectByIds(roomIds);
        socketRooms.forEach(socketRoom -> {
            if (flag) {
                socketRoom.setStatus(SocketRoomStatus.审核);
                redisService.addMap(RedisConstant.Audit_Room_Status+ShiroUtil.getTenantCode(),socketRoom.getId(),socketRoom.getId());
            } else {
                socketRoom.setStatus(SocketRoomStatus.正常);
                redisService.removeMapField(RedisConstant.Audit_Room_Status+ShiroUtil.getTenantCode(),socketRoom.getId());
            }
            socketRoomMapper.updateByPrimaryKey(socketRoom);
        });
    }

}
